package project.parser.ast.stats;

import project.environments.DynamicEnv;
import project.environments.StaticEnv;
import project.parser.ast.aux.Args;
import project.parser.ast.exps.Exp;

import static project.parser.tokenizer.TokenType.*;

public class Print implements Stat {
	private final Args args;

	public Print(Args args) {
		this.args = args;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder().append(PRINT).append(OPEN_PAR);
		sb.append(args.toString());
		sb.append(CLOSED_PAR).append(SEMI_COL);
		return sb.toString();
	}

	@Override
	public void typecheck(StaticEnv senv) {
		args.typecheckList(senv);
	}

	@Override
	public void evaluate(DynamicEnv denv) {
		int i = 0, size = args.length();
		for (Exp exp : args) {
			System.out.print(exp.evaluate(denv));
			if (++i != size)
				System.out.print(" ");
		}
		System.out.println();
	}
}
